import { TextLoader } from "langchain/document_loaders/fs/text";
import { PDFLoader } from "langchain/document_loaders/fs/pdf";
import { DocxLoader } from "langchain/document_loaders/fs/docx";
import { CharacterTextSplitter } from "langchain/text_splitter";
import {getCollection} from '../chroma/index.js'
import {files} from '../db/models.js'
import {AIMessage, HumanMessage} from 'langchain/schema'
import {cheeseChat, openaiChat} from '../chatModel/index.js'
// async function generate(text, messages, is_bots, callback, endCallback) {
//     try {
//         const url = "http://43.134.44.205:30625/context_batch_return";
//         const data = {
//             text: text,
//             message: messages,
//             is_bots: is_bots
//         };
//         const response = await fetch(url, {
//             method: "POST",
//             body: JSON.stringify(data),
//             headers: {
//                 "Content-Type": "application/json"
//             }
//         });
//         const reader = response.body.getReader();
//         const decoder = new TextDecoder('utf-8');
//         let last_index = 0;
//         let buffer = '';
//         while (true) {
//             try{
//                 const { done, value } = await reader.read()
//                 if (done) {
//                     endCallback({ status: 'end' });
//                     break;
//                 }
//                 buffer += decoder.decode(value);
//                 const delimiterIndex = buffer.indexOf('\0');
//                 if (delimiterIndex !== -1) {
//                     let decodedText = buffer.substring(0, delimiterIndex);
//                     buffer = '';
//                     try {
//                         decodedText = JSON.parse(decodedText);
//                         if (decodedText.status === 'Success') {
//                             callback({
//                                 status: 'success',
//                                 text: decodedText.text.slice(last_index)
//                             });
//                             last_index = decodedText.text.length;
//                         } else if (decodedText.status === 'Fail') {
//                             endCallback({ status: 'end' });
//                             break;
//                         }
//                     } catch (e) {
//                         callback({
//                             status: 'error',
//                             msg: e.name + "：" + e.message,
//                             res: decodedText
//                         });
//                         // console.log(e)
//                         // console.log(decodedText)
//                     }
//                 }
//             }catch (e) {
//                 callback({
//                     status: 'error',
//                     msg: e.name + "：" + e.message,
//                 });
//                 endCallback({ status: 'end' });
//                 break;
//             }
//         }
//     } catch (e) {
//         console.log(e)
//         callback({
//             status: 'error',
//             msg: '模型未上线！',
//         });
//         endCallback({ status: 'end' });
//     }
// }
async function generate(text, history, callback, endCallback) {
    const messages = []
    history.forEach(mess=>{
        switch (mess.role){
            case 'ai':
                messages.push(new AIMessage(mess.message))
                break
            case 'me':
                messages.push(new HumanMessage(mess.message))
                break
        }
    })
    messages.push(new HumanMessage(text))
    await cheeseChat.call(messages,{
        callbacks: [
            {
                handleLLMNewToken(token) {
                    if(!!token.length){
                        callback({
                            status: 'success',
                            text: token
                        });
                    }
                },
            },
        ]
    })
    endCallback({ status: 'end' });
}
const splitter = new CharacterTextSplitter({separator: "。",});
async function docEmbedding(fileInfo){
    const fileName = fileInfo.name;
    const ext = fileName.slice(fileName.lastIndexOf('.') + 1);
    let loader = null;
    switch(ext){
        case 'txt':
            loader = new TextLoader(fileInfo.path);
            break
        case 'pdf':
            loader = new PDFLoader(fileInfo.path, {
                pdfjs: () => import("pdfjs-dist/legacy/build/pdf.js").then(m => m.default),
            });
            break
        case 'docx':
            loader = new DocxLoader(fileInfo.path);
            break
    }
    let docs = await loader.load();
    const output = await splitter.splitDocuments(docs)
    output.map(item => {
        item.pageContent = item.pageContent.replace(/(\n)|(\r)|(\f)|( )/g, "")
        return item
    })
    const vectorStore = getCollection(fileInfo.knowledgeBase)
    const ids = await vectorStore.addDocuments(output)
    await files.updateOne({ _id: fileInfo._id }, { $set: { ids: ids } });
}

export { generate, docEmbedding };
